package util;

import Controller.MainViewController;
import DAO.MyDrawPane;
import DAO.NodeList;
import DAO.TreeNode;
import javafx.event.EventHandler;
import javafx.geometry.Bounds;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;

import static util.LayoutUtil.SelectItem;

/**
 * 对点击事件做出处理，保存被选中节点
 */
public class NodeChooseUtil {

    public static TreeNode node = new TreeNode();   //选中的节点
    private static TreeNode rnode = new TreeNode();  //选中且被右键的点
    private static int menuExists = 0;  //限制菜单的出现

    // 单击选中
    public static void Click() {

        // 添加鼠标点击事件
        MyDrawPane.drawPane.setOnMouseClicked(event -> {
            //判断是否是左键
            if (event.getButton() == MouseButton.PRIMARY) {
                double x = event.getSceneX();  //获取鼠标点击事件的场景坐标，即相对于场景的 x 和 y 坐标。
                double y = event.getSceneY();
                for (int i = 0; i < NodeList.list.size(); i++) {
                    TreeNode n = new TreeNode();
                    n = NodeList.list.get(i);
                    n.setClick(false);
                    Bounds b = n.getLayoutBounds();
                    Bounds bd = n.localToScene(b);   // 取出组件的最大和最小的x，y坐标
                    n.setStyle(
                            "-fx-background-color:#d6ecf0;" +
                                    "-fx-background-radius:10;" +
                                    "-fx-padding:10;"
                    );

                    if (bd.getMinX() <= x && bd.getMaxX() >= x && bd.getMinY() <= y && bd.getMaxY() >= y) {  //检查鼠标点击的坐标是否在节点的边界框内。
                        n.setClick(true);
                        node = n;
                        n.setStyle(
                                "-fx-background-color:#faff72;" +
                                        "-fx-background-radius:10;" +
                                        "-fx-padding:10;"
                        );
                    }
                }
                menuExists=0;
            }
            //判断是否是右键
            if (event.getButton() == MouseButton.SECONDARY) {
                double x = event.getSceneX();  //获取鼠标点击事件的场景坐标，即相对于场景的 x 和 y 坐标。
                double y = event.getSceneY();
                for (int i = 0; i < NodeList.list.size(); i++) {
                    TreeNode n = new TreeNode();
                    n = NodeList.list.get(i);
                    Bounds b = n.getLayoutBounds();
                    Bounds bd = n.localToScene(b);   // 取出组件的最大和最小的x，y坐标

                    if (n.isClick() == true && bd.getMinX() <= x && bd.getMaxX() >= x && bd.getMinY() <= y && bd.getMaxY() >= y) {  //检查鼠标点击的坐标是否在节点的边界框内。
                        //不存在菜单才展示
                        if (menuExists==0){
                            menuExists=1;
                            rnode = n;
                            // 菜单项
                            MenuItem item1 = new MenuItem("删除");
                            MenuItem item2 = new MenuItem("编辑");
                            MenuItem item3 = new MenuItem("添加子节点");
                            MenuItem item4 = new MenuItem("添加兄弟节点");

                            item1.setOnAction(e -> {
                                //删除
                                TreeUtil.deleteNode(rnode);
                                MyDrawPane.draw();
                                MyTreeView.setTreeView();
                            });
                            item2.setOnAction(e -> {
                                //编辑
                                MyTextArea.showTextArea(rnode);
                            });
                            item3.setOnAction(e -> {
                                //添加子节点
                                TreeUtil.addNode(rnode);
                                SelectItem(MainViewController.item);
                                MyDrawPane.draw();
                                MyTreeView.setTreeView();
                            });
                            item4.setOnAction(e -> {
                                //添加兄弟节点
                                TreeNode p = NodeList.getParent(rnode);
                                if (rnode != NodeList.getRoot()) {
                                    TreeUtil.addNode(p);
                                }
                                SelectItem(MainViewController.item);
                                MyDrawPane.draw();
                                MyTreeView.setTreeView();
                            });


                            ContextMenu contextMenu = new ContextMenu();
                            contextMenu.getItems().addAll(item1, item2, item3, item4);
                            // 显示右键菜单
                            contextMenu.show(rnode, event.getScreenX(), event.getScreenY());
                        }
                    }
                }
            }
        });
    }

    //鼠标双击事件：监听节点，双击出现文本输入框，编辑节点文字内容
    public static void DoubleClick() {
        MyDrawPane.drawPane.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                if (event.getClickCount() == 2) {
                    double x = event.getSceneX();
                    double y = event.getSceneY();

                    for (int i = 0; i < NodeList.list.size(); i++) {
                        TreeNode node = new TreeNode();
                        node = NodeList.list.get(i);
                        Bounds b = node.getLayoutBounds();
                        Bounds bd = node.localToScene(b);

                        if (bd.getMinX() <= x && bd.getMaxX() >= x && bd.getMinY() <= y && bd.getMaxY() >= y) {
                            // 双击出现文本输入框，编辑节点文字内容
                            MyTextArea.showTextArea(node);
                        }
                    }
                }
                MyTreeView.setTreeView();
            }
        });
    }
}


